package com.honey.mybatis.generator.dependent;

import java.util.ArrayList;
import java.util.List;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.Method;
import com.honey.compilation.xml.XmlElement;
import com.honey.core.compiler.CompilationException;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.notify.Signal;
import com.honey.mybatis.generator.AbstractMybatis;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.dao.DaoInterface;
import com.honey.mybatis.generator.dao.ProduceDaoInterface;
import com.honey.mybatis.generator.dao.field.NameSpaceField;
import com.honey.mybatis.generator.dao.method.CountMethod;
import com.honey.mybatis.generator.dao.method.DeleteByDynamicMethod;
import com.honey.mybatis.generator.dao.method.DeleteByPrimaryKeyMethod;
import com.honey.mybatis.generator.dao.method.ExistsMethod;
import com.honey.mybatis.generator.dao.method.InsertMethod;
import com.honey.mybatis.generator.dao.method.InsertSelectiveMethod;
import com.honey.mybatis.generator.dao.method.SaveMethod;
import com.honey.mybatis.generator.dao.method.SelectAllMethod;
import com.honey.mybatis.generator.dao.method.SelectByDynamicMethod;
import com.honey.mybatis.generator.dao.method.SelectByPrimaryKeyMethod;
import com.honey.mybatis.generator.dao.method.SelectOneByDynamicMethod;
import com.honey.mybatis.generator.dao.method.UpdateByDynamicMethod;
import com.honey.mybatis.generator.dao.method.UpdateByDynamicSelectiveMethod;
import com.honey.mybatis.generator.dao.method.UpdateByPrimaryKeyMethod;
import com.honey.mybatis.generator.dao.method.UpdateByPrimaryKeySelectiveMethod;

public class ProduceInjectionDependentDaoInterface extends ProduceDaoInterface {
	
	public ProduceInjectionDependentDaoInterface(MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(belongPlugin, introspectSchema, introspectSchemaColumn);
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractProduceJavaCompilation#produceCompilation()
	 */
	@Override
	public AbstractJavaCompilation produceCompilation() throws CompilationException {
		abstractJavaCompilation = new DaoInterface(mybatisPlugin, introspectSchema, introspectSchemaColumn).createJavaCompilation();
//		abstractJavaCompilation.setSuperClass(
//			new AbstractMybatis(mybatisPlugin ){
//				public  String getSuperType(){
//					return super.getBaseDaoInterfaceClassName(null) +"<"+super.getEntityPrimitiveClassName(introspectSchema.getSchema())+">"; 
//				}
//			}.getSuperType()
//		);
		snedBeanSignalToSpringPlugin();
		
		return abstractJavaCompilation;
	}
	
	@Override
	public Field[] produceFields() throws CompilationException {
		return new Field[]{
			new NameSpaceField(null, mybatisPlugin, introspectSchema, introspectSchemaColumn).createField()
		};
	}
	
	@Override
	public Method[] produceMethods() throws CompilationException {
		List<Method> answer = new ArrayList<Method>();
		AbstractJavaCompilation abstractJavaCompilation = getAbstractJavaCompilation();
		
		answer.add( new CountMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new CountMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
		
		answer.add( new ExistsMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new ExistsMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
	
		
		answer.add( new SelectByPrimaryKeyMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new SelectByPrimaryKeyMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
	
		answer.add( new SelectAllMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new SelectAllMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,false,true).createMethod() );
		answer.add( new SelectAllMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true,false).createMethod() );
		answer.add( new SelectAllMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true,true).createMethod() );
		
		answer.add( new SelectByDynamicMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new SelectByDynamicMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,false,true).createMethod() );
		answer.add( new SelectByDynamicMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true,false).createMethod() );
		answer.add( new SelectByDynamicMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true,true).createMethod() );
		
		answer.add( new SelectOneByDynamicMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new SelectOneByDynamicMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
		
		answer.add( new DeleteByDynamicMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new DeleteByDynamicMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
		
		answer.add( new DeleteByPrimaryKeyMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new DeleteByPrimaryKeyMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
		
		answer.add( new InsertMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new InsertMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
		
		answer.add( new InsertSelectiveMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new InsertSelectiveMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
		
		answer.add( new SaveMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new SaveMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
		
		answer.add( new UpdateByPrimaryKeyMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new UpdateByPrimaryKeyMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
		
		answer.add( new UpdateByPrimaryKeySelectiveMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new UpdateByPrimaryKeySelectiveMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
		
		answer.add( new UpdateByDynamicSelectiveMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new UpdateByDynamicSelectiveMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
		
		answer.add( new UpdateByDynamicMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod() );
		answer.add( new UpdateByDynamicMethod(abstractJavaCompilation, mybatisPlugin, introspectSchema, introspectSchemaColumn,true).createMethod() );
		
		return answer.toArray( new Method [answer.size()] );
	}
	
	protected void snedBeanSignalToSpringPlugin(){
		//向Spring插件发送配置bean的通知
		Signal signal = new Signal();
		XmlElement springBean = new XmlElement("bean");
		springBean.addAttribute("id", abstractJavaCompilation.getType().getShortName());
		springBean.addAttribute("class", 
			new AbstractMybatis(mybatisPlugin ){
				public  String getImplementsType(){
					return super.getImplementsClassType(introspectSchema.getSchema(),null).getLongName(); 
				}
			}.getImplementsType()
		);
		XmlElement property = new XmlElement(springBean,"property");
		property.addAttribute("name","baseMybatisdao").addAttribute("ref","baseMybatisdao");
		
//		property.addAttribute("name","baseMybatisdao").addAttribute("ref",
//			new AbstractMybatis(mybatisPlugin ){
//				public  String getBaseDaoImplementsClassType(){
//					return super.getBaseDaoImplementsClassType(introspectSchema.getSchema(),null).getLongName(); 
//				}
//			}.getBaseDaoImplementsClassType()
//		);
		signal.setAttachment(springBean);
		super.snedSignal(signal);
	}
}
